﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include <tchar.h>
#ifdef _DEBUG
#include <crtdbg.h>
#endif
#include <WinSock2.h>
#include "Ucop/Typedefs.h"
#include "Ucop/ErrorHandler.h"
#include "Ucop/System/Error.h"
#include "Game/GameApplication.h"
using Ucop::Char;
using Ucop::ErrorHandler;

LONG WINAPI __UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo);


int _tmain(int _Argc, Char* _Argv[])
{
    using namespace Ucop;

	int _Rc = GameApplication::EXIT_OK;
    {
        /* Filter all unhandled exception like NULL-Pointer exception */
        ::SetUnhandledExceptionFilter(__UnhandledExceptionFilter);

        /* Initialize WIN32 Sockets */
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData))
        {
            _Lprintf(_L("WSAStartup() failed, %s"), 
                System::GetErrorString(System::GetLastError()).c_str());
            return -1;
        }

        /* Run application */
        {
			GameApplication _App;
            _Rc = _App.Run(_Argc, _Argv);
        }

        /* Uninitialize WIN32 Sockets */
        WSACleanup();

#ifdef _DEBUG
        _CrtDumpMemoryLeaks();
#endif
    }

    return _Rc;
}